$(function(){
	util.ready();
		$.ajax({
			url:context+'/authority/user!getCurrentUserInfo.action',
			success:function(data){
				$("#user_id").val(data.id);
				$("#login_name").html(data.login_name);
				$("#user_name").val(data.name);
				$("#user_email").val(data.email);
				$("#user_phone").val(data.phone);
			}
		})
	$("body").append(html);
       $("body").append(html_userinfo);
       $("#modify_passwd").click(
       	function(){
       	$("#mywindow").show();
		show_window("mywindow");
		}
	);
       $("#modify_userinfo").click(
       	function(){
       	$("#user_windows").show();
			show_window("user_windows");
		}
	);
       
    //0:成功1:两次新密码不相同 2:原密码错误 3:修改失败
    $("#pwd_confirm").click(
       	function(){
       	var ajax_sub = $("#password_form").form('validate');
       	if(ajax_sub){
       		$.ajax({
				url:context+'/authority/user!modifyPassword.action',		
				data:{password:$("#password").val(),new_password:$("#new_password").val(),r_new_password:$("#r_new_password").val()},
				success:function(data){
					if(data!=null&&data == '0'){
						 $('#mywindow').window('close');
						 $("body").append(success_html("密码修改成功!"));
						 show_window("success_html");
					}else if(data!=null&&data == '1'){
						$('#mywindow').window('close');
						$("body").append(success_html("密码修改失败,输入的两次新密码不相同!"));
					}else if(data!=null&&data == '2'){
						$('#mywindow').window('close');
						$("body").append(success_html("密码修改失败,原始密码错误!"));
					}else{
						$('#mywindow').window('close');
						$("body").append(success_html("密码修改失败!"));
						}
						show_window("success_html");
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					if(textStruts == "error"){
						$('#mywindow').window('close');
						$("body").append(success_html("密码修改失败!或者用户失效,请重新登陆!"));
						show_window("success_html");
					}
				}
			});
       	}
    });
    	//用户资料修改
        $("#user_confirm").click(
       	function(){
       	var ajax_subm = $("#user_form").form('validate');
       	if(ajax_subm){
       		$.ajax({
				url:context+'/authority/user!modifyUserInfo.action',		
				data:{id:$("#user_id").val(),user_name:$("#user_name").val(),user_email:$("#user_email").val(),user_phone:$("#user_phone").val()},
				success:function(data){
					if(data!=null&&data == '0'){
						 $('#user_windows').window('close');
						 $("body").append(success_html("用户信息修改成功!"));
						 show_window("success_html");
					}else if(data!=null&&data == '1'){
						$('#user_windows').window('close');
						$("body").append(success_html("用户信息修改失败!"));
					}
						show_window("success_html");
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					if(textStruts == "error"){
						$('#user_windows').window('close');
						$("body").append(success_html("失败!或者用户失效,请重新登陆!"));
						show_window("success_html");
					}
				}
			});
       	}
    });
    $("#pwd_cancel").click(
       	function(){
       		$('#mywindow').window('close');
       	});
    $("#user_cancel").click(
       	function(){
       		$('#user_windows').window('close');
       	});
})
function show_window(id){
	 $("#password").val("");
	 $("#new_password").val("");
	 $("#r_new_password").val("");
	 $('#'+id).window({
			modal:true,
  			width:480,
    		iconCls:'icon-ok',
    		minimizable:false,
    		onClose: function(){
    		},
    		onOpen:function(){
			}
		});
		$('#'+id).window('open');
}

var html ="<div id='mywindow' title='修改密码' class='easyui-window' closed='true' align='center' style='display:none'>"+
"<form id='password_form' name='password_form' action='' method='post'>"+	
"<table cellpadding='10' cellspacing='10'>"+
		"<tr>"+
			"<td>原密码：</td>"+
			"<td><input type='password' size='20' id='password' class='easyui-validatebox' required='true' validType='length[3,20]'></td>"+
		"</tr>"+
		"<tr>"+
			"<td>新密码：</td>"+
			"<td><input type='password' size='20' id='new_password' class='easyui-validatebox' required='true' validType='length[3,20]'></td>"+
		"</tr>"+
		"<tr>"+
			"<td>确认新密码：</td>"+
			"<td><input type='password' size='20' id='r_new_password' class='easyui-validatebox' required='true' validType='length[3,20]'></td>"+
		"</tr>"+
		"<tr>"+
			"<td><input type='button' id='pwd_confirm' value='确定' class='tj_1'></td>"+
			"<td><input type='button' id='pwd_cancel' value='取消' class='tj_1'></td>"+
		"</tr>"+
	"</table>"+
	"</form>"+
"</div>";
function success_html(succ){
	$("#success_html").remove();
	return "<div id='success_html' title='资料修改' class='easyui-window' closed='true' align='center'>" +
	"<table cellpadding='30' cellspacing='30'>"+
	"<tr>"+
	"<td>"+succ+"!</td>"+
	"</tr>"+
	"</table>"+
	"</div>";
}
var html_userinfo ="<div id='user_windows' title='修改用户资料' class='easyui-window' closed='true' align='center' style='display:none'>"+
"<form id='user_form' name='user_form' action='' method='post'>"+	
"<table cellpadding='10' cellspacing='10'>"+
		"<tr>"+
			"<td>用户名：</td>"+
			"<input type='hidden' id='user_id'/>"+
			"<td id='login_name'> </td>"+
		"</tr>"+
		"<tr>"+
			"<td>姓名：</td>"+
			"<td><input type='text' size='20' id='user_name' value='' class='easyui-validatebox' required='true' validType='length[2,20]'></td>"+
		"</tr>"+
		"<tr>"+
			"<td>Email：</td>"+
			"<td><input type='text' size='20' id='user_email' value=''  class='easyui-validatebox' required='true' validType='length[3,20]'></td>"+
		"</tr>"+
		"<tr>"+
			"<td>手机号码：</td>"+
			"<td><input type='text' size='20' id='user_phone' value=''  class='easyui-validatebox' required='true' validType='length[11,11]'></td>"+
		"</tr>"+
		"<tr>"+
			"<td><input type='button' id='user_confirm' value='确定' class='tj_1'></td>"+
			"<td><input type='button' id='user_cancel' value='取消' class='tj_1'></td>"+
		"</tr>"+
	"</table>"+
	"</form>"+
"</div>";
